const app = getApp()
/*
 * fun [function] 需要防抖的函数
 * delay [number] 毫秒，防抖期限值
 */
function debounce(fun, delay = 300) {
  let timer;
  return function () {
    let ctx = this
    let args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fun.apply(ctx, args)
    }, delay)
  }
}
/**
 * @desc 防抖函数
 * @param {需要防抖的函数} func
 * @param {延迟时间} wait
 * @param {是否立即执行} immediate
 */
 function debounces(func, wait, immediate) {
  let timeout
  return function(...args) {
    let context = this
    if (timeout) clearTimeout(timeout)
    if (immediate) {
      let callNow = !timeout
      timeout = setTimeout(function() {
        timeout = null
      }, wait)
      if (callNow) func.apply(context, args)
    } else {
      timeout = setTimeout(function() {
        func.apply(context, args)
      }, wait)
    }
  }
}
/*
 * fun [function] 需要节流的函数
 * delay [number] 毫秒，节流期限值
 */
function throttle(fun, interval = 300) {
    let last;
    let timer;
    return function () {
        let ctx = this
        let args = arguments
        let now = new Date()
        if (last && now - last < interval) {
          clearTimeout(timer)
          timer = setTimeout(function () {
            last = now
          }, interval)
        } else {
          last = now
          fun.apply(ctx, args)
        }
    }
}

// 时间排序功能    点击升序降序
function isSort(type, arr) {
  if (type) {
    arr.sort((x, y) => {
      return Date.parse(x.update) - Date.parse(y.update)
    })
  } else {
    arr.sort((x, y) => {
      return Date.parse(y.update) - Date.parse(x.update)
    })
  }
}

//     搜索过滤
async function Filter(value,prevArr){
  // console.log(value)
  // console.log(prevArr)
  const result = prevArr.filter(res =>{
    // console.log(res)
    for(var key in res){
      if((res[key]+'').includes(value))
      return res
    }
    // if(res.customer_name === null) return
    // return res.customer_name.includes(value)
  })
  return result
}


//     拨打电话  
function callTel(phone){
  wx.makePhoneCall({
    phoneNumber: phone,
    fail: ()=>{
      return 0;
    }
  })
}

//   显示消息框
function toast(str,icon = 'none',delay = 1500){
  wx.showToast({
    title: str,
    icon,
    duration: delay,
    mask: true
  });
}

/*
 * date [function] 时间戳或者时间标准
 * fmt [number] 时间格式
 */
function format(date, fmt = 'YYYY-MM-DD') {

	let newDate = date;
	let fmts = fmt;

	//fmts='YYYY-MM-DD HH:mm:ss'
	if (!newDate) {
		newDate = new Date;
	}
	if (typeof newDate === 'number') {
		newDate = new Date(newDate);
	}
	const o = {
		'M+': newDate.getMonth() + 1,
		'D+': newDate.getDate(),
		'h+': newDate.getHours() % 12 === 0 ? 12 : newDate.getHours() % 12,
		'H+': newDate.getHours(),
		'm+': newDate.getMinutes(),
		's+': newDate.getSeconds(),
		'q+': Math.floor((newDate.getMonth() + 3) / 3),
		S: newDate.getMilliseconds(),
	};
	const week = {
		0: '\u65e5',
		1: '\u4e00',
		2: '\u4e8c',
		3: '\u4e09',
		4: '\u56db',
		5: '\u4e94',
		6: '\u516d',
	};
	if (/(Y+)/.test(fmts)) {
		fmts = fmts.replace(RegExp.$1, (`${newDate.getFullYear()}`).substr(4 - RegExp.$1.length));
	}
	if (/(E+)/.test(fmts)) {
		fmts = fmts.replace(RegExp.$1, () => {
			if (RegExp.$1.length > 1) {
				if (RegExp.$1.length > 2) {
					return `\u661f\u671f${week[newDate.getDay()]}`;
				}
				return `\u5468${week[newDate.getDay()]}`;
			}
			return week[`${newDate.getDay()}`];
		});
	}
	Object.keys(o).forEach((k) => {
		if (new RegExp(`(${k})`).test(fmts)) {
			fmts = fmts.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)));
		}
	});
	return fmts;
};

export {
  debounce,
  throttle,
  isSort,
  Filter,
  callTel,
  toast,
  format,
  debounces
}